import request from '@/router/axios';
import {baseUrl} from '@/config/env';
import securityApi from "@/api/start/securityApi";
import startCrypto from "@/start/startCrypto";
import securityConfig from "@/config/securityConfig";
import {getStore, setStore} from "@/utils/store";
import website from "@/config/website";

const SecurityDataUrlKey = website.SecurityData + "_url";
const SecurityDataClientIdKey = website.SecurityData + "_client_id";

export function isSecurityDataIgnoreUrl(url) {
    let urls = getStore({name: SecurityDataUrlKey}) || [securityConfig.securityIgnoreUrl];
    if (!urls || urls.length < 1) return false;
    for (let i = 0; i < urls.length; i++) {
        let temp = urls[i]
        if (url.indexOf(temp) > -1) {
            return true;
        }
    }
    return false
}

export function getSecurityData() {
    return getStore({name: website.SecurityData})
}
export function getSecurityDateClientId(){
    return getStore({name: SecurityDataClientIdKey})
}

const securityTools = {

    state: {
        permissionMap: {},
        securityData: getStore({name: website.SecurityData}),
        securityDataIgnoreUrl: getStore({name: SecurityDataUrlKey}) || [securityConfig.securityIgnoreUrl],
    },
    mutations: {
        SET_SECURITY_DATA(state, data) {
            setStore({name: website.SecurityData, content: data.clientPublicKey, expire: data.expire})
        },
        SET_SECURITY_DATA_IGNORE_URL(state, data) {
            setStore({name: SecurityDataUrlKey, content: data.ignoreUrls})
        },
        SET_SECURITY_DATA_CLIENT_ID(state, data) {
            setStore({name: SecurityDataClientIdKey, content: data.clientId, expire: data.expire})
        }
    },
    actions: {
        getUserPermission() {
            return new Promise(resolve => {
                request({
                        url: baseUrl + '/user/menu/getUserPermissions',
                        method: 'get',
                    }
                ).then(res => {
                    let resData = res.data.data;
                    if (!resData) {
                        resolve();
                        return;
                    }
                    let temp = {};
                    for (let i = 0; i < resData.length; i++) {
                        if (!resData[i].code) continue;
                        temp[resData[i].code] = resData[i]
                    }
                    securityTools.state.permissionMap = temp;
                    resolve()
                });
            })
        },
        hasPermission(permission, defaultState = false) {
            if (securityTools.state.permissionMap == null) return defaultState;
            let r = securityTools.state.permissionMap[permission]
            if (r === null || r === undefined) return defaultState
            return r;
        },
        loadSecurityData({commit}) {
            return new Promise((resolve, reject) => {
                let enable= securityConfig.securityEnable;
                console.log("securityConfig.securityEnable",enable)
                if (enable===false) {
                    resolve()
                    return
                }
                let old = securityTools.state.securityData;
                if (old) {
                    resolve()
                    return;
                }
                let data = startCrypto.sm2.encrypt(securityConfig.clientPrivateKey, securityConfig.clientPublicKey)
                securityApi.getClientDecryptionKey(data).then(res => {
                    commit('SET_SECURITY_DATA', res.data.data)
                    commit('SET_SECURITY_DATA_IGNORE_URL', res.data.data)
                    commit('SET_SECURITY_DATA_CLIENT_ID', res.data.data)
                }).catch(err => {
                    console.log("err", err)
                    reject(err)
                })
            })
        },

    }
};
export default securityTools;
